"""Setup the ${project} application"""
import logging

from paste.deploy import appconfig
from pylons import config

from authtest.config.environment import load_environment
from authkit.users.sqlalchemy_driver import UsersFromDatabase

log = logging.getLogger(__name__)

def setup_config(command, filename, section, vars):
    """Place any commands to setup authtest here"""
    confuri = "config:" + filename
    if ":" in section:
        confuri += "#" + section.rsplit(":", 1)[-1]
    conf = appconfig(confuri)

    load_environment(conf.global_conf, conf.local_conf)

    # Load the models
    from authtest import model
    model.meta.metadata.bind = model.meta.engine

    users = UsersFromDatabase(model)
    print "Creating tables"

    # Create the tables if they aren't there already
    model.meta.metadata.create_all(checkfirst=True)

    print "Adding users and roles"
    users.role_create(u"admin")
    users.user_create(u"admin", password=u"ce3cdc48892e221ee218c9a9e7f761a2") #pass: admin
    users.user_add_role(u"admin", role=u"admin")

    # save the new values into the db
    model.meta.Session.commit()

